﻿using System;
using System.Collections.Generic;
using System.DAL;
using System.Dto;
using System.IBLL;
using System.IDAL;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace System.BLL
{
    public class RepairManager : IRepairManager
    {
        public async Task AddOneUserGuarantee(GetUserGuaranteeListDto listDto)
        {
            using(IUserGuaranteeService userGuaranteeSvc = new UserGuaranteeService())
            {
                Models.Models.UserGuarantee data = new Models.Models.UserGuarantee()
                {
                    RepairTitle = listDto.RepairTitle,
                    PepairContent = listDto.RepairContent,
                    Remark = listDto.Remark,
                    EstimatedTime = DateTime.Parse(listDto.EstimatedTime)
                };
                if (!(listDto.UserName == "" || listDto.UserName == null))
                {
                    data.UserId = int.Parse(listDto.UserName);
                }
                await userGuaranteeSvc.CreateAsync(data);
            }
        }

        public GetGuaranteeListViewDto GetGuaranteeListData()
        {
            using (IUserGuaranteeService userGuaranteeSvc = new UserGuaranteeService())
            {
                var data = userGuaranteeSvc.GetAllAsync().ToList();
                GetGuaranteeListViewDto viewDto = new GetGuaranteeListViewDto()
                {
                    AllCount = data.Count.ToString(),
                    AllSuccessCount = data.Where(m => m.RepairState == true).ToList().Count.ToString(),
                    thisCount = data.Where(m=>m.CreateTime>DateTime.Now.Date&&m.CreateTime<DateTime.Now.AddDays(1).Date).ToList().Count.ToString(),
                    thisSuccessCount = data.Where(m=>m.CreateTime>DateTime.Now.Date&&m.CreateTime<DateTime.Now.AddDays(1).Date&&m.RepairState==true).ToList().Count.ToString()
                };
                return viewDto;
            }
        }

        public List<(int Id, string UserName)> GetHouseholdNameLists(int limit, int page, string HouseholdName, out int datalet)
        {
            using (IHouseholdService householdSvc = new HouseholdService())
            {
                var data = householdSvc.GetAllAsync().ToList().Select(m =>  (m.Id, m.UserName)).ToList();

                if (!(HouseholdName == "" || HouseholdName == null))
                {
                    data = data.Where(m => m.UserName.Contains(HouseholdName)).ToList();
                }

                datalet = data.Count();
                data = data.OrderBy(m => m.Id).Skip((page - 1) * limit).Take(limit).ToList();
                return data;
            }
        }

        public List<GetUserGuaranteeListDto> GetMyUserGuaranteeLists(string userName, int limit, int page, out int datalet, string RepairTitle, string RepairContent)
        {
            var data = GetUserGuaranteeLists(limit,page,out datalet, RepairTitle,RepairContent);
            data.Where(m => m.UserName == userName).ToList();
            datalet = data.Count();
            return data;
        }

        public List<GetUserGuaranteeListDto> GetUserGuaranteeLists(int limit, int page, out int datalet, string RepairTitle, string RepairContent, string UserName=null)
        {
            using (IUserGuaranteeService userGuaranteeSvc = new UserGuaranteeService())
            {
                using(IHouseholdService householdSvc = new HouseholdService())
                {
                    var data = userGuaranteeSvc.GetAllAsync().Select(m => new GetUserGuaranteeListDto()
                    {
                        Id = m.Id,
                        RepairTitle = m.RepairTitle,
                        RepairContent = m.PepairContent,
                        CreateTime = m.CreateTime.ToString(),
                        UserName = m.UserId.ToString(),
                        RepairState = m.RepairState==null?"--": (bool)m.RepairState?"修理成功":m.RepairContent,
                        Remark = m.Remark,
                        RepairResult = m.RepairResult==null?"--":(bool)m.RepairResult?"已受理":m.RepairContent,
                        EstimatedTime = m.EstimatedTime.ToString()
                    }).ToList();
                    foreach(var list in data)
                    {
                        list.UserName = list.UserName == "" ? "工作人员" : householdSvc.GetOneByIdAsync(int.Parse(list.UserName)).UserName;
                        list.EstimatedTime = DateTime.Parse(list.EstimatedTime).ToString("yyyy-MM-dd HH:mm");
                        list.CreateTime = DateTime.Parse(list.CreateTime).ToString("yyyy-MM-dd");
                    }

                    if (!(RepairTitle == "" || RepairTitle == null))
                    {
                        data = data.Where(m => m.RepairTitle.Contains(RepairTitle)).ToList();
                    }
                    if (!(RepairContent == "" || RepairContent == null))
                    {
                        data = data.Where(m => m.RepairContent.Contains(RepairContent)).ToList();
                    }
                    if (!(UserName == "" || UserName == null))
                    {
                        data = data.Where(m => m.UserName.Contains(UserName)).ToList();
                    }

                    datalet = data.Count();
                    data = data.OrderBy(m => m.Id).Skip((page - 1) * limit).Take(limit).ToList();
                    return data;
                }
            }
        }

        public async Task IsAcceptanceById(int id, bool repairResult, string repairContent)
        {
            using (IUserGuaranteeService userGuaranteeSvc = new UserGuaranteeService()) 
            {
                var data = userGuaranteeSvc.GetOneByIdAsync(id);
                data.RepairResult = repairResult;
                if (!(repairContent == null || repairContent == ""))
                {
                    data.RepairContent = "拒绝受理原因：" + repairContent;
                }
                await userGuaranteeSvc.EditAsync(data);
            }
        }

        public async Task SuccessOneDataById(int id,bool repairState, string repairContent="")
        {
            using (IUserGuaranteeService userGuaranteeSvc = new UserGuaranteeService())
            {
                var data = userGuaranteeSvc.GetOneByIdAsync(id);
                data.RepairState = repairState;
                if (!(repairContent == null || repairContent == ""))
                {
                    data.RepairContent = "维修失败原因：" + repairContent;
                }
                await userGuaranteeSvc.EditAsync(data);
            }
        }

        public async Task WriteEvaluateById(int id, string Evaluate)
        {
            using(IUserGuaranteeService userGuaranteeSvc = new UserGuaranteeService())
            {
                var data = userGuaranteeSvc.GetOneByIdAsync(id);
                data.Evaluate = Evaluate;
                await userGuaranteeSvc.EditAsync(data);
            }
        }
    }
}
